﻿using System;
using System.Linq;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
using System.ServiceModel;

using ChatForum.Database;
using ChatForum.Domain;
using ChatForum.DAL;
using RESTContract = ChatForum.REST.ServiceContract;
using RESTService = ChatForum.Service.REST;

using SOAPContract = ChatForum.SOAP.ServiceContract;
using SOAPService = ChatForum.Service.SOAP;
using ChatForum.Messaging;
using System.Collections.Specialized;
using System.Configuration;
using ChatForum.WCF;


namespace ChatForum.ServiceHost.Console
{
    class Program
    {
        private static string[] baseUris = new string[]
        {
            "http://" + Environment.MachineName + "/services/",
        };

        private static DatabaseBase database;
        private static IRepository repository;
        private static BusinessLogic logic;
        private static SmtpServerBase smtpServer = null;

        static void Main(string[] args)
        {
            Init();
            InitServices();
        }

        private static void Init()
        {
            NameValueCollection appSettings = ConfigurationManager.AppSettings;

            string dataSource = appSettings["dataSource"];
            string databaseName = appSettings["databaseName"];
            string userId = appSettings["userId"];
            string password = appSettings["password"];
            string smtpHost = appSettings["smtpHost"];
            int smtpPort = Int32.TryParse(appSettings["smtpPort"], out smtpPort) ? smtpPort : 587;
            string smtpUserId = appSettings["smtpUserId"];
            string smtpPassword = appSettings["smtpPassword"];

            database = new SqlServerDatabase(dataSource, databaseName, userId, password, null);
            repository = new ChatForumRepository(database);
            smtpServer = new SmtpServerBase(smtpHost, smtpPort, smtpUserId, smtpPassword);
            logic = new BusinessLogic(repository);
        }
        private static void InitServices()
        {
            HostManager.Instance["IAccountService.REST"] = REST<RESTService.AccountService, RESTContract.IAccountService>.Host(baseUris
                                                            , new InstanceProvider<RESTService.AccountService>(new RESTService.AccountService(logic)));

            HostManager.Instance["IForumService.REST"] = REST<RESTService.ForumService, RESTContract.IForumService>.Host(baseUris
                                                            , new InstanceProvider<RESTService.ForumService>(new RESTService.ForumService(logic)));

            HostManager.Instance["IAccountService.SOAP"] = SOAP<SOAPService.AccountService, SOAPContract.IAccountService>.Host(baseUris
                                                            , new InstanceProvider<SOAPService.AccountService>(new SOAPService.AccountService(logic, smtpServer))
                                                            , true);

            HostManager.Instance["IForumService.SOAP"] = SOAP<SOAPService.ForumService, SOAPContract.IForumService>.Host(baseUris
                                                            , new InstanceProvider<SOAPService.ForumService>(new SOAPService.ForumService(logic))
                                                            , true);

            HostManager.Instance.StartServices();
            System.Console.WriteLine("The services started..");
            System.Console.ReadKey();
            HostManager.Instance.StopServices();
        }
    }
}
